package com.jzo2o.market.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.market.enums.ActivityStatusEnum;
import com.jzo2o.market.enums.CouponStatusEnum;
import com.jzo2o.market.mapper.ActivityMapper;
import com.jzo2o.market.mapper.CouponMapper;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.dto.request.ActivityQueryForPageReqDTO;
import com.jzo2o.market.model.dto.request.ActivitySaveReqDTO;
import com.jzo2o.market.model.dto.response.ActivityInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author LeeC
 * @description
 * @date 2025/2/11
 * @PACKAGE_NAME com.jzo2o.market.service.impl
 */
@Service
public class ActivityServiceImpl implements IActivityService {
    @Resource
    private ActivityMapper activityMapper;
    @Resource
    private CouponMapper couponMapper;
    @Override
    public void save(ActivitySaveReqDTO activitySaveReqDTO) {
        activitySaveReqDTO.check();
        int status = 0;
        // 判断活动状态
        // 待生效
        if(LocalDateTime.now().isBefore(activitySaveReqDTO.getDistributeStartTime())){
            status = ActivityStatusEnum.NO_DISTRIBUTE.getStatus();
        }
        // 已失效
        else if(LocalDateTime.now().isBefore(activitySaveReqDTO.getDistributeEndTime())){
            status = ActivityStatusEnum.LOSE_EFFICACY.getStatus();
        }
        // 进行中
        else if(LocalDateTime.now().isBefore(activitySaveReqDTO.getDistributeEndTime())
                && LocalDateTime.now().isAfter(activitySaveReqDTO.getDistributeStartTime())){
            status = ActivityStatusEnum.DISTRIBUTING.getStatus();
        }
        Activity bean = BeanUtil.toBean(activitySaveReqDTO, Activity.class);
        bean.setStatus(status);
        bean.setStockNum(bean.getTotalNum());
        if(bean.getTotalNum() == 0){
            bean.setTotalNum(1);
        }
        Activity activity = activityMapper.selectById(bean.getId());
        if(ObjectUtil.isNull(activity)){
            activityMapper.save(bean);
        }else{
            activityMapper.updateById(bean);
        }
    }

    @Override
    public PageResult<ActivityInfoResDTO> page(ActivityQueryForPageReqDTO pageReqDTO) {
        PageHelper.startPage(pageReqDTO.getPageNo().intValue(), pageReqDTO.getPageSize().intValue());
        List<ActivityInfoResDTO> page = activityMapper.page(pageReqDTO);
        Long total = (long) page.size();
        Long pages = total % pageReqDTO.getPageSize() ==0 ?total / pageReqDTO.getPageSize():total / pageReqDTO.getPageSize()+1;
        return new PageResult<>(pages, total , page);
    }

    @Override
    public ActivityInfoResDTO getById(Long id) {
        return activityMapper.getById(id);
    }

    @Override
    @Transactional
    public void revoke(Long id) {
        Activity activity = activityMapper.selectById(id);
        if(ObjectUtil.isNull(activity)){
            throw new RuntimeException("活动不存在");
        }
        if(activity.getStatus() == ActivityStatusEnum.VOIDED.getStatus()){
            throw new RuntimeException("活动已作废");
        }
        activity.setStatus(ActivityStatusEnum.VOIDED.getStatus());
        int i = activityMapper.updateById(activity);
        if(i == 0){
            throw new RuntimeException("活动撤销失败");
        }
        Boolean b = couponMapper.updateByActivityID(CouponStatusEnum.INVALID.getStatus(), id);
        if(!b){
            throw new RuntimeException("活动撤销失败");
        }
    }
}
